import {Injectable} from '@angular/core';
import {Chero} from '../../model/chero';
import {CHEROES} from '../../mock-data/cheroes';

@Injectable()
export class CheroService {
  static nextCrisisId = 100;

  constructor() {
  }

  // https://embed.plnkr.co/?show=preview   网络请求的例子   --https://angular.io/tutorial/toh-pt6


  getCHeros() {
    return Promise.resolve(CHEROES);
  }

  getCHero(id: number | string) {
    return Promise.resolve(CHEROES)
      .then(crises => crises.find(crisis => crisis.id === +id));
  }

  addCHero(name: string) {
    name = name.trim();
    if (name) {
      const cherott = new Chero(CheroService.nextCrisisId++, name);
      Promise.resolve(CHEROES).then(res => res.push(cherott));
    }
  }
}
